//
//  CZMainViewController.swift
//  swiftweibo
//
//  Created by Apple on 15/11/23.
//  Copyright © 2015年 tzw. All rights reserved.
//

import UIKit
/// 自定义tabbar
class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置文字高亮
//        tabBar.tintColor = UIColor.orangeColor()

        /// 首页
        let homeVc = CZHomeViewController()
        addChildViewController(homeVc, title: "首页", imageName: "tabbar_home")
        
        
        // 消息
        let messageVc = CZMessageViewController()

        addChildViewController(messageVc, title: "消息", imageName: "tabbar_message_center")
        
        // 占位控制器
        addChildViewController(UIViewController())
        
        // 发现
        let discoverVc = CZDiscoverViewController()

        addChildViewController(discoverVc, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileVc = CZProfileViewController()

        addChildViewController(profileVc, title: "我", imageName: "tabbar_profile")
    }
    func addChildViewController(controller: UIViewController,title: String,imageName: String) {

        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        // 拼接高亮的图片名称
        let hilightedImageName = imageName + "_highlighted"
        // 设置选中的按钮
        // 让选中的图片按照原来的颜色显示
        controller.tabBarItem.selectedImage = UIImage(named: hilightedImageName)?.imageWithRenderingMode( UIImageRenderingMode.AlwaysOriginal)
        
        // 设置选中的文字颜色和大小 
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Highlighted)
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 设置frame
        let width = tabBar.bounds.width/5
        let frame = CGRect(x: 2 * width, y: 0, width: width, height: tabBar.bounds.size.height)
        composeButton.frame = frame;
    }
    // 监听撰写按钮的点击事件
    func composeClick(){
    print(__FUNCTION__)
    }
    // 懒加载撰写按键
    lazy var composeButton: UIButton = {
        let button = UIButton()
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 把按钮添加到tabbar里面
            self.tabBar.addSubview(button)
        // 添加按钮点击
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()

  }
